const conf = {
  data: {
    hasEmptyGrid: false,
    incidentList:[
      {tag:1,time :'7:00-8:00',name:'语文',notes:true,recommend:true},     
      {tag:2,time :'8:00-9:00',data:["第一条数据内容内容","第二条数据内容内容","第三条数据内容内容"]},
      {tag:1,time :'9:00-10:00',name:'数学',notes:true,recommend:false},
      {tag:1,time :'10:00-11:00',name:'数学',notes:true,recommend:false},
      {tag:1,time :'11:00-12:00',name:'数学',notes:false,recommend:true},
      {tag:2,time :'14:00-15:00',data:["第四条数据内容内容","第五条数据内容内容"]}
      //备注：tag=1 课程  tag=2 自定义
    ]
  },
  getTodayMsg(e){
    //console.log(e.currentTarget.dataset.idx+1)
   
    const get_year = this.data.cur_year;
    const get_month = this.data.cur_month;
    const get_day = e.currentTarget.dataset.idx+1;
    console.log(get_year+"|"+get_month+"|"+get_day)
    this.setData({
       cur_date:get_day
    });
  },
  getSystemInfo() {
    try {
      const res = wx.getSystemInfoSync();   
      this.setData({
        scrollViewHeight: res.windowHeight -100|| 667
      });
    } catch (e) {
      console.log(e);
    }
  },
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();   //返回一个月总天数 
   
  },
  getFirstDayOfWeek(year, month) {  
    return new Date(Date.UTC(year, month - 1, 1)).getDay(); //返回1号是星期几
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month); //返回1号是星期几
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);   //前面加空天数
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];

    const thisMonthDays = this.getThisMonthDays(year, month); //返回一个月总天数 

    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
    }

    this.setData({
      days
    });
    
  },
  selectUser(){ //选择宝贝
    wx.navigateTo({
      url: '../selectUser/selectUser'
    })
  },
  toDoday(){  //回到今天
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_date = date.getDate();
    this.setData({
        cur_year,  //年
        cur_month, //月
        cur_date //天
      })
  },
  addNewCourse(){ //选择宝贝
    wx.navigateTo({
      url: '../addNewCourse/addNewCourse'
    })
  },
  onLoad(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_date = date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.getSystemInfo();
    this.setData({
      cur_year,  //年
      cur_month, //月
      cur_date,  //天
      weeks_ch   //星期
    })
   
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;  //得到 data-handle 值
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth); //重新计算天数
      this.calculateEmptyGrids(newYear, newMonth); //重新计算空格

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  },
  onShareAppMessage() {
    return {
      title: '小程序日历',
      desc: '还是新鲜的日历哟',
      path: 'pages/index/index'
    }
  }
};

Page(conf);
